<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="330" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="80px" label-position="left">
			<el-form-item label="排序号">
				<el-input-number v-model="form.sortNo" clearable placeholder="排序号"></el-input-number>
			</el-form-item>
			<el-form-item label="分类名称" prop="boxName">
				<el-input v-model="form.boxName" clearable placeholder="分类名称"></el-input>
			</el-form-item>
			<el-form-item label="父路径" prop="parentId">
				<el-cascader v-model="form.parentId" :options="bpmCollect" :props="bpmCollectProps" :show-all-levels="false" clearable></el-cascader>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增分类',
				edit: '编辑分类'
			},
			visible: false,
			isSaveing: false,
			form: {
				sortNo:0,
				model:'1',
				isDoc:'2',
				sortId:"",
				name: "",
				parentId: ""
			},
			rules: {
				name: [
					{required: true, message: '请输入分类名称'}
				]
			},
			bpmCollect: [],
			bpmCollectProps: {
				value: "boxId",
				label: "boxName",
				emitPath: false,
				checkStrictly: true
			}
		}
	},
	mounted() {
		this.getBpmCollectBoxForTree()
	},
	methods: {
		//显示
		open(mode='add'){
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//获取字典列表
		async getBpmCollectBoxForTree(){
			var res = await this.$API.bpm.bpmCollect.getBpmCollectBoxForTree.get({model:"1",isDoc:"2"});
			if(res.code==200)
			{
				this.bpmCollect = [{boxId:"0",boxName:"请选择"}].concat(res.data);
			}else
			{
				this.bpmCollect = [{boxId:"0",boxName:"请选择"}]
			}
		},
		//表单提交方法
		submit(){
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res
					if(this.mode=="add")
					{
						res = await this.$API.bpm.bpmCollect.insertBpmCollectBox.post(this.form);
					}else
					{
						res = await this.$API.bpm.bpmCollect.updateBpmCollectBox.post(this.form);
					}
					this.isSaveing = false;
					if(res.code == 200){
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					}else{
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		//表单注入数据
		setData(data){
			this.form.sortNo = data.sortNo
			this.form.boxId = data.boxId
			this.form.model = data.model
			this.form.isDoc = data.isDoc
			this.form.boxName = data.boxName
			this.form.parentId = data.parentId
		}
	}
}
</script>

<style>
</style>
